package com.heatup.apt.management.util;
import java.util.ArrayList;  
import java.util.List;  
import org.apache.http.HttpResponse;  
import org.apache.http.NameValuePair;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.DefaultHttpClient;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.util.EntityUtils;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.JSONObject;  
  
/** 
 * 生成短网址并返回 
 * @author: Jerri  
 * @date: 2014年3月22日下午9:58:54 
 */  
public class GenerateShortUrlUtil {  
    public static DefaultHttpClient httpclient;  
    static {  
        httpclient = new DefaultHttpClient();  
        //httpclient = (DefaultHttpClient) HttpClientConnectionManager  
              //  .getSSLInstance(httpclient); // 接受任何证书的浏览器客户端  
    }  
      
    /** 
     * 生成端连接信息 
     *  
     * @author: Jerri  
     * @date: 2014年3月22日下午5:31:15 
     */  
    public static String  generateShortUrl(String url) {  
        try {  
            HttpPost httpost = new HttpPost("http://dwz.cn/create.php");  
            List<NameValuePair> params = new ArrayList<NameValuePair>();  
            params.add(new BasicNameValuePair("url", url)); // 用户名称  
            httpost.setEntity(new UrlEncodedFormEntity(params,  "utf-8"));  
            HttpResponse response = httpclient.execute(httpost);  
            String jsonStr = EntityUtils  
                    .toString(response.getEntity(), "utf-8");  
            //System.out.println(jsonStr);  
            JSONObject object = JSON.parseObject(jsonStr);  
            System.out.println(object.getString("tinyurl"));  
            return object.getString("tinyurl");  
        } catch (Exception e) {  
            e.printStackTrace();  
            return "";  
        }  
          
    }  
      
    /** 
     * 测试生成端连接 
     * @param args 
     * @author: Jerri  
     * @date: 2014年3月22日下午5:34:05 
     */  
    public static void main(String []args){  
        generateShortUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe4dfc45a9f29fb8b&redirect_uri=http%3A%2F%2Ftest.heatup.cn%2Fapi%2Fredirect%2FweiXinJump%3Fpath%3D%2Fhome%2Fcoupon_list%26coupon_code%3D0024d4l0019B0351%26scopetepm%3Dsnsapi_base&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect");  
    }  
}  